package com.penghaisoft.microwms.base.web.data.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.base.web.data.service.IMdPurchaseService;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.MdPurchase;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping(value = "/data/purchase")
@Slf4j
public class MdPurchaseControler extends BaseController {
	
	@Autowired
	private IMdPurchaseService mdPurchaseService;
	
	/**
     * 新增记录
     */
    @PostMapping("c")
    public Resp create(MdPurchase mdPurchase){
    	Resp resp = new Resp();
    	mdPurchase.setActiveFlag("!0");
    	if(mdPurchaseService.existence(mdPurchase)){
    		resp.setCode(Constant.FAIL);
    		resp.setMsg("货主名称冲突");
    		return resp;
    	}
        mdPurchase.preInsert();
        mdPurchase.setActiveFlag("1");
        mdPurchase.setCreateBy(getLoginName());
        mdPurchase.setLastModifiedBy(getLoginName());
        mdPurchase.setGmtCreate(new Date());
        resp = mdPurchaseService.create(mdPurchase);
        resp.setMsg("添加成功");
        return resp;
    }
    /**
     * 删除记录（逻辑删除active_flag=0)
     */
    @PostMapping("d")
    public TransResult delete(MdPurchase mdPurchase) {
    	mdPurchase.preUpdate();
        Resp resp = mdPurchaseService.delete(mdPurchase);
        return fromResp(resp);
    }
    /**
     * 查询列表
     */
    @PostMapping("list")
    public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, MdPurchase mdPurchase) {
    	mdPurchase.setActiveFlag("!0");
        Pager<MdPurchase> result = mdPurchaseService.findListByCondition(page, rows, mdPurchase);
        return success(result);
    }
    /**
     * 查询单条
     */
    @GetMapping("r/{id}")
    public TransResult queryById(@PathVariable String id) {
    	MdPurchase mdPurchase = mdPurchaseService.findById(id);
        return success(mdPurchase);
    }
    /**
     * 修改记录
     *
     * @param mdProModel
     * @return
     */
    @PostMapping("u")
    public Resp update(MdPurchase mdPurchase) {
    	mdPurchase.preUpdate();
    	mdPurchase.setLastModifiedBy(getLoginName());
        Resp resp = mdPurchaseService.update(mdPurchase);
        return resp;
    }
    /**
     * 
     */
    @GetMapping("getPurchase")
    public TransResult getPurchase() {
        return mdPurchaseService.queryPurchase();
    }
}
